package com.axonactive.yolo.service.internal;

import java.util.List;

import com.axonactive.yolo.dto.Flight;
import com.axonactive.yolo.util.CONSTANTS;

/**
 * This class provides the methods for filtering
 * @author pqtrung
 *
 */
public class FlightFilterService {
	/**
	 * Filter by airline name, it's different to filter by airline code in FlightFactoryService 
	 * @param flights
	 * @param airlines
	 * @return list of flight
	 */
	public static List<Flight> filterByAirlines(List<Flight> flights,String airlineNames)
	{
		String[] airlines=airlineNames.split(CONSTANTS.TOKEN_DELIMITER);
		for (String airline : airlines) {
			for (int i = 0; i < flights.size(); i++) {
				if (!isAirlineContain(flights.get(i), airline))
				{
					flights.remove(i);
					i--;
				}
			}
		}
		return flights;
	}
	/**
	 * Check flight has first carrier named by airline
	 * @param flight flight need to be checked
	 * @param airline name compared
	 * @return true if airline is belong to the first journey of flight, otherwise return false
	 */
	public static boolean isAirlineContain(Flight flight,String airline)
	{
		if (flight.isRoundTrip())
		{
			if (airline.equals(flight.getOutBound().getDepartAirports().get(0).getAirline().getName()) && airline.equals(flight.getInBound().getDepartAirports().get(0).getAirline().getName()))
			{
				return true;
			}
		}
		else
		{//one way
			if (airline.equals(flight.getOutBound().getDepartAirports().get(0).getAirline().getName()))
			{
				return true;
			}
		}
		return false;
		
	}

}
